from django.db import models
from django import forms

import datetime
import os

from django.conf import settings
from django.core.files import storage
from django.db import models
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _
# Create your models here.


class t_location_prefix(models.Model):
    PREFIX = models.CharField(max_length="20", primary_key=True)
    LOCATION = models.CharField(max_length="100")
    COUNTRY = models.CharField(max_length="50")
    TYPE = models.SmallIntegerField()

class Report(models.Model):
    Destination = models.CharField(max_length="100")
    DestinationCode = models.CharField(max_length="50")
    VendorRefNo = models.CharField(max_length="50")
    VendorName = models.CharField(max_length="50")
    Date = models.CharField(max_length="50")
    Day = models.CharField(max_length="50")
    Attempts = models.IntegerField()
    Completed = models.IntegerField()
    RawDur = models.DecimalField(max_digits=10, decimal_places=2)
    RndDur = models.DecimalField(max_digits=10, decimal_places=2)
    Cost = models.DecimalField(max_digits=6, decimal_places=4)
    CostPerMin = models.DecimalField(max_digits=6, decimal_places=4)

class ReportSum(models.Model):
    Date = models.CharField(max_length="50", null=True)
    Country = models.CharField(max_length="50", null=True)
    Type = models.IntegerField(null=True)
    VendorName = models.CharField(max_length="50", null=True)
    Attempts = models.IntegerField(null=True)
    Completed = models.IntegerField(null=True)
    ASR = models.FloatField(null=True)
    RawDur = models.FloatField(null=True)
    ACD = models.FloatField(null=True)
    MinCode = models.FloatField(null=True)
    AvgCode = models.FloatField(null=True)
    MaxCode = models.FloatField(null=True)


class ReportGraph(models.Model):
    Country = models.CharField(max_length="50", null=True)
    Attempts = models.IntegerField(null=True)
    Completed = models.IntegerField(null=True)
    ASR = models.FloatField(null=True)
    ACD = models.FloatField(null=True)
    RawDur = models.FloatField(null=True)

class Person (models.Model):
    "Defines the model for the person entity"
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    phone = models.CharField(max_length=20)
    age = models.IntegerField()
    comments = models.TextField()

    def __unicode__(self):
        return u'%s %s' % (self.first_name, self.last_name)


class FileSystemStorage(storage.FileSystemStorage):
    """
    Subclass Django's standard FileSystemStorage to fix permissions
    of uploaded files.
    """
    def _save(self, name, content):
        name =  super(FileSystemStorage, self)._save(name, content)
        full_path = self.path(name)
        mode = getattr(settings, 'FILE_UPLOAD_PERMISSIONS', None)
        if not mode:
            mode = 0644
        os.chmod(full_path, mode)
        return name
   

class Upload(models.Model):
    """Uploaded files."""
    file = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT), upload_to='uploads', help_text=_(u"""The file itself."""))
    timestamp = models.DateTimeField(default=datetime.datetime.now)
    
    class Meta:
        ordering = ['-timestamp',]
    
    def __unicode__(self):
        return u"%s" % (self.file)
    
    @property
    def size(self):
        return filesizeformat(self.file.size)
